<?php
namespace App\Http\Controllers\Admin\Qudao;

use App\Service\Admin\Qudao\CostService;
use Illuminate\Http\Request;

class CostController extends BaseController
{
    //费用

    public function __construct(CostService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //保存
    public function add(Request $request)
    {
        $pas['type'] = $request->post('type', 0);
        $pas['cost'] = $request->post('cost', 0);
        if (!$pas['type']) {
            return $this->toJson(-2, '类型必须的');
        }
        if (!$pas['cost']) {
            return $this->toJson(-2, '额度必须的');
        }
        $pas['cost'] *= 100;
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}